import http from '../httpService';
import { ProductPagedQueryDto } from './dto/productPagedQueryDto';
import { ProductCommentPagedQueryDto } from './dto/productCommentPagedQueryDto';
import { PagedList } from '../dto/pagedList';
import { ProductListDto } from './dto/productListDto';

import { ProductCommentInputDto } from "./dto/productCommentInputDto";
import { ProductCommentListDto } from './dto/productCommentListDto';


class ProductService {
    public async getProductPagedListAsync(input: ProductPagedQueryDto): Promise<PagedList<ProductListDto>> {
        let url_ = '/api/services/app/Product/GetProductPagedListAsync?'

        if (input.keyWords !== undefined)
            url_ += 'KeyWords=' + encodeURIComponent('' + input.keyWords) + '&';
        if (input.categoryIds !== undefined)
            input.categoryIds && input.categoryIds.forEach(item => { url_ += 'CategoryIds=' + encodeURIComponent('' + item) + '&'; });
        if (input.recommendIds !== undefined)
            input.recommendIds && input.recommendIds.forEach(item => { url_ += 'RecommendIds=' + encodeURIComponent('' + item) + '&'; });
        if (input.sorting !== undefined)
            url_ += 'Sorting=' + encodeURIComponent('' + input.sorting) + '&';
        if (input.skipCount !== undefined)
            url_ += 'SkipCount=' + encodeURIComponent('' + input.skipCount) + '&';
        if (input.maxResultCount !== undefined)
            url_ += 'MaxResultCount=' + encodeURIComponent('' + input.maxResultCount) + '&';
        url_ = url_.replace(/[?&]$/, '');
        let result = await http.get(url_, {});
        return result.data.result;
    }

    public async getProductAsync(id: number) {
        return await http.get('/api/services/app/Product/GetProductAsync', { id: id });
    }

    public async getProductCommentPagedListAsync(input: ProductCommentPagedQueryDto): Promise<PagedList<ProductCommentListDto>> {
        let result = await http.get('/api/services/app/Product/GetProductCommentPagedListAsync', input);
        return result.data.result
    }

    public async getProductCommentStatisticsAsync(id: number) {
        return await http.get('/api/services/app/Product/GetProductCommentStatisticsAsync', { id: id });
    }

    public async getProductListByIds(ids: string[]) {
        let url_ = '/api/services/app/Product/GetProductListByIdsAsync?'
        if (ids !== undefined)
            ids && ids.forEach(item => { url_ += 'ids=' + encodeURIComponent('' + item) + '&' })
        url_ = url_.replace(/[?&]$/, '')
        let result = await http.get(url_, {})
        return result
    }

    public async createProductComment(model: ProductCommentInputDto) {
        let url_ = '/api/services/app/Product/CreateProductCommentAsync'
        return await http.post(url_, model)
    }
}

export default new ProductService()